package com.sunsharing.common;


import com.sunsharing.common.config.Config;
import com.sunsharing.common.config.NodeModel;
import com.sunsharing.common.serialize.support.nativejava.NativeJavaSerialization;
import com.sunsharing.common.utils.StringUtils;
import org.apache.log4j.Logger;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Queue;

/**
 * Created with IntelliJ IDEA.
 * User: criss
 * Date: 13-3-31
 * Time: 下午4:25
 * To change this template use File | Settings | File Templates.
 */
public class Context implements Serializable {

    static transient Logger logger = Logger.getLogger(Context.class);

    //消息类型
    char msgType;
    //任务号
    String taskId;

    //消息报文
    Msg msg;
    //List Router
    //List<List<String>> router = new ArrayList<List<String>>();
    //请求方节点
    String apiNode;
    //当fromNode为空时表示这个请求的第一个节点
    String beginNode;

    public Context clone()
    {
        Context con = new Context(msgType,msg);
        con.taskId = taskId;
        con.apiNode = apiNode;
        con.beginNode = beginNode;
        con.history = history;
        return con;
    }


    //浏览历史
    String history = "";

    int retry = 0;

    public void addRetry()
    {
        retry++;
    }

    public boolean stopRety()
    {
        if(retry>=10)
        {
            return true;
        }else
        {
            return false;
        }
    }

    public byte[] serialization()
    {
        try
        {
            //重试变成0
            retry = 0;
            ByteArrayOutputStream o = new  ByteArrayOutputStream();
            NativeJavaSerialization out = new NativeJavaSerialization();
            out.serialize(o).writeObject(this);
            return o.toByteArray();
        }catch (Exception e)
        {
            logger.error("序列化失败",e);
            throw new RuntimeException("序列化失败");
        }
    }

    public Context(char msgType,Msg msg)
    {
        try
        {
            this.msg = msg;
            this.msgType = msgType;
            if(StringUtils.isBlank(taskId))
            {
                taskId = StringUtils.genUUID();
            }
        }catch (Exception e)
        {
            logger.error("转换字符集异常",e);
            throw new RuntimeException("转换字符集异常");
        }
    }
//    @Override
//    public String toString()
//    {
//        return msg.toString();
//    }

    public char getMsgType() {
        return msgType;
    }

    public void setMsgType(char msgType) {
        this.msgType = msgType;
    }



    public String getTaskId() {
        return taskId;
    }

    public void setTaskId(String taskId) {
        this.taskId = taskId;
    }


    public String getBeginNode() {
        return beginNode;
    }

    public void setBeginNode(String beginNode) {
        this.beginNode = beginNode;
    }

    public boolean isFirstRequest()
    {
        if(StringUtils.isBlank(beginNode))
        {
            return true;
        } else
        {
            return false;
        }
    }

    public Msg getMsg() {
        return msg;
    }

    public void setMsg(Msg msg) {
        this.msg = msg;
    }

    public boolean isLocal()
    {
        if(msgType==Constant.MSG_TYPE_RESULT && beginNode.equals(Config.getInstance().getNodeId()))
        {
             return true;
        }else if(msgType==Constant.MSG_TYPE_REQUEST)
        {
            //结果报文
            List<NodeModel> nodes = Config.getInstance().getNodesByServiceName(msg.serviceId);
            String myNode = Config.getInstance().getNodeId();
            for(NodeModel node:nodes)
            {
                if(node.nodeId.equals(myNode))
                {
                    return true;
                }
            }
        }
        return false;
    }

    public String getApiNode() {
        return apiNode;
    }

    public void setApiNode(String apiNode) {
        this.apiNode = apiNode;
    }



    public synchronized void addHistory(String nodeId)
    {
        this.history+="->"+nodeId;
    }

//    public List<String> getHistory() {
//        return history;
//    }
//
//    public  void setHistory(List<String> history) {
//        this.history = history;
//    }


    public String getHistory() {
        return history;
    }

    public void setHistory(String history) {
        this.history = history;
    }

    public void printHistory()
    {
        logger.info("路由地址:::::::"+this.history);
    }
}
